package com.scau.beyondboy.criminalintent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import java.util.UUID;
/**
 * A simple {@link Fragment} subclass.
 */
public class CrimeFragment extends Fragment
{
    private static final String TAG ="CrimeFragment" ;
    public static final String EXTRA_CRIME_ID = "criminalintent.CRIME_ID";
    private Crime mCrime;
    private EditText mTitleField;
    private Button mDateButton;
    private CheckBox mSolvedCheckBox;
    public CrimeFragment()
    {
        // Required empty public constructor
    }
    /**
     * 创建一个携带参数的CrimeFragment实例
     * @param crimeId 出入一个Crime的Id
     * @return 返回CrimeFragment实例
     */
    public static CrimeFragment newInstance(UUID crimeId)
    {
        Bundle bundle=new Bundle();
        bundle.putSerializable(EXTRA_CRIME_ID,crimeId);
        CrimeFragment crimeFragment=new CrimeFragment();
        crimeFragment.setArguments(bundle);
        return  crimeFragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        Log.i(TAG,"onCreate" );
        UUID crimeId=(UUID)getArguments().getSerializable(EXTRA_CRIME_ID);
        mCrime=CrimeLab.get(getActivity()).getCrime(crimeId);
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        Log.i(TAG,"onCreateView");
        View root=inflater.inflate(R.layout.fragment_crime, container, false);
        mTitleField =(EditText)root.findViewById(R.id.crime_title);
        mDateButton=(Button)root.findViewById(R.id.crime_date);
        mDateButton.setEnabled(false);
        mTitleField.setText(mCrime.getTitle());
        mDateButton.setText(mCrime.getDate().toString());
        mSolvedCheckBox=(CheckBox)root.findViewById(R.id.crime_solved);
        mSolvedCheckBox.setChecked(mCrime.isSolved());
        mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                mCrime.setSolved(isChecked);
            }
        });
        mTitleField.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                mCrime.setTitle(s.toString());
            }
            @Override
            public void afterTextChanged(Editable s)
            {
            }
        });
        return root;
    }
}
